package com.settlement.system.controller;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblAcsRoleBillStatus;
import com.settlement.system.model.entity.common.TblBillResponsibleReason;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblAcsRoleBillStatusService;
import com.settlement.system.service.ITblBillResponsibleReasonService;
import com.settlement.system.common.util.common.FasUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 损失原因/责任方/单据状态对应表 前端控制器
 * @author xiaochuan
 * @since 2020-06-11
 */

@RestController
@RequestMapping("/bill-responsible-reason")
@Api(tags = "损失原因/责任方/单据状态",description = "损失原因/责任方/单据状态")
@Slf4j
public class TblBillResponsibleReasonController extends ExcelController<TblBillResponsibleReason> {

    @Resource
    private ITblBillResponsibleReasonService tblBillResponsibleReasonService;

    @Resource
    private ITblAcsRoleBillStatusService tblAcsRoleBillStatusService;

    @Override
    public IBaseService init() {
        return tblBillResponsibleReasonService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblBillResponsibleReason.class;
    }

    /**
     * 获取单据状态不包含系统归档数据
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation("获取损失原因")
    @GetMapping("getResponsibleReason")
    public ObjectDataResponse getResponsibleReason(HttpServletRequest request) throws Exception {
        Map<String,Object> result = new HashMap<>(16);
        Map<String, Object> params = buildParam(request);
        try {
            params.put("billStatusNameNotIn","('系统归档')");
            List<TblBillResponsibleReason> rows  = tblBillResponsibleReasonService.getResponsibleReason(params);
            result.put("rows",rows);
        } catch (ServiceException e) {
            log.error("损失原因/责任方/单据状态对应表异常",e);
            return new ObjectDataResponse(500,"损失原因/责任方/单据状态对应表异常");
        }
        return new ObjectDataResponse<Map>(result,200,"获损失原因/责任方/单据状态对应表成功");
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        String billStatusNoList = (String)model.get("billStatusNoList");
        model.put("billStatusNoList", FasUtil.formatInQueryCondition(billStatusNoList));
    }

    @Override
    protected void setModelValue(TblBillResponsibleReason model) throws Exception {
        String responsibleNo = model.getResponsibleNo();
        String lossReasonNo = model.getLossReasonNo();
        if(StringUtils.isEmpty(responsibleNo)){
            responsibleNo = "RSP_"+tblBillResponsibleReasonService.getBillNo("responsibleNo");
            model.setResponsibleNo(responsibleNo);
        }
        if(StringUtils.isEmpty(lossReasonNo)){
            lossReasonNo = "LRS_"+tblBillResponsibleReasonService.getBillNo("lossReasonNo");
            model.setLossReasonNo(lossReasonNo);
        }
    }

    @Override
    protected void saveData(List<TblBillResponsibleReason> datas) throws ServiceException {
        if (!CollectionUtils.isEmpty(datas)){
            for (int i = 0; i <datas.size() ; i++) {
                assembleModel(datas.get(i),i);
            }
        }
        super.saveData(datas);
    }

    private void assembleModel(TblBillResponsibleReason s,int i) throws ServiceException{
        Map<String,Object> params = new HashMap<>(16);
        params.put("billStatusName",s.getBillStatusName());
        try {
            List<TblAcsRoleBillStatus> bsList = tblAcsRoleBillStatusService.selectByParam(params);
            if(!CollectionUtils.isEmpty(bsList)){
                Map<String,Object> orderParams = new HashMap<>(16);
                orderParams.put("billStatusName",s.getOrderBillStatusName());
                List<TblAcsRoleBillStatus> bsOrderList = tblAcsRoleBillStatusService.selectByParam(orderParams);
                s.setBillStatusNo(bsList.get(0).getBillStatusNo());
                s.setOrderBillStatusNo(bsOrderList.get(0).getBillStatusNo());
                s.setResponsibleNo("RSP_"+(tblBillResponsibleReasonService.getBillNo("responsibleNo")+i));
                s.setLossReasonNo("LRS_"+(tblBillResponsibleReasonService.getBillNo("lossReasonNo")+i));
            }else{
                throw new ServiceException("单据状态【"+s.getBillStatusName()+"】不存在，请先配置单据信息");
            }
        } catch (ServiceException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    @ApiOperation("删除主表和明细操作")
    @RequestMapping(value = "deleteByLossReasonNo",method = {RequestMethod.POST})
    @ResponseBody
    protected ObjectDataResponse delete(@RequestBody TblBillResponsibleReason tblBillResponsibleReason, HttpServletRequest request){
        try {
            tblBillResponsibleReasonService.deleteByLossReasonNo(tblBillResponsibleReason);
            return new ObjectDataResponse(200,"删除成功");
        } catch (Exception ex){
            log.info("删除数据异常",ex);
            return new ObjectDataResponse(500,"删除失败，请联系管理员！");
        }
    }


    @Override
    protected List<TblBillResponsibleReason> getExportDate(Map<String, Object> params) throws ServiceException {
        return tblBillResponsibleReasonService.selectMainAndDtlByParam(params);
    }

}

